package org.taiolib.indicators.movingAverage;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Michael G. Langer
 */
public class WeightedMovingAverage implements MAStrategy {

    public List<BigDecimal> movingAverage(LinkedList<BigDecimal> inSerie, int value, int shift, MathContext mContext) {
        
        List<BigDecimal> result = new LinkedList<BigDecimal>();
        BigDecimal sum = new BigDecimal(0);
        BigDecimal divider = new BigDecimal( ((value*(value+1))/2), mContext);

        for (int i = 0; i <= inSerie.size()-value; i++) {

            for ( int j = 0; j < value; j++ ) {
                sum = sum.add( inSerie.get(i+j).multiply(new BigDecimal(value-j)) );
            }
            
            sum = sum.divide( divider );
            result.add(new BigDecimal(sum.doubleValue(), mContext));
        }

        return result;
    }

    public MovingAverageType getType() {
        return MovingAverageType.WEIGHTED;
    }

}
